package com.chris.libs.example.pages.ext.http;

import android.view.View;

import com.chris.dev.base.libs.v1.http.params.IdParam;
import com.chris.libs.base.x.mvpp.mvpp.DefaultPresenter;
import com.chris.libs.example.api.UserApi;
import com.chris.libs.example.common.http.DefaultHttp;
import com.chris.libs.http.x.v1.NetApi;

import javax.inject.Inject;

import okhttp3.RequestBody;

/**
 * @author Chris Chan
 * Create on： 2025-02-03 06:00
 * Use for: Build 业务逻辑处理
 * Explain:
 */
public class HttpPresenter extends DefaultPresenter<HttpContract.UIView>
        implements HttpContract.Presenter {
    // 数据中心
    private HttpViewModel viewModel;

    // Dagger2注入构造方法
    @Inject
    public HttpPresenter() {
    }

    // 传入数据中心
    public void setViewModel(HttpViewModel viewModel) {
        this.viewModel = viewModel;
    }

    // 实例方法，一般由ViewProxy主动调用，必须在契约接口中定义
    @Override
    public void requestAndUpdate() {
        this.viewModel.setInfo("请求并更新的数据");
    }

    @Override
    public void getUserInfo() {
        // 用法1 需要定义方法
//        UserApi.getUserInfo(IdParam.create(123457), new NetCallback<NetResult<UserInfo>>() {
//            @Override
//            protected void onComplete(NetResponse<NetResult<UserInfo>> netResponse) {
//                viewModel.setUserInfo(netResponse.data.data);
//            }
//        });

        // 用法2 不需要定义方法
//        UserApi.service
//                .getUserInfo(IdParam.create(123457))
//                .enqueue(new StandardCallback<NetResult<UserInfo>>() {
//                    @Override
//                    protected void onResult(NetResponse<NetResult<UserInfo>> response, boolean isSuccess, NetResult<UserInfo> result) {
//                        UserInfo userInfo = result.data;
//                        userInfo.name = userInfo.name + " 来了哦";
//                        viewModel.setUserInfo(userInfo);
//                    }
//                });
        // 用法3 简洁
//        UserApi.service
//                .getUserInfo(IdParam.create(47369))
//                .enqueue(new DefaultCallback<>((resp, isSuccess, data) -> System.out.println("")));

        // 用法4 统一封装post
//        DefaultHttp.post(UserApi.service.getUserInfo(IdParam.create(32094895)), (resp, isSuccess, data) -> {
//            System.out.println("");
//        });

    }

    @Override
    public void getUserInfo(View dataView) {
        // 用法5 默认成功，并且有状态展示
//        UserApi.service
//                .getUserInfo(IdParam.create(73149))
//                .enqueue(new SuccessNetCallback<UserInfo>(dataView) {
//                    @Override
//                    protected void onSuccess(UserInfo data) {
//                        data.name = data.name + " come on";
//                        viewModel.setUserInfo(data);
//                    }
//                });
        // 用法6 带状态的简化版
//        DefaultHttp.post(UserApi.service.getUserInfo(IdParam.create(12138)), dataView, (response, isSuccess, data) -> {
//            data.name = data.name + " come here";
//            viewModel.setUserInfo(data);
//        });

        // 用法7 携带头部token
//        String token = "Berer thisismytoken";
//        RequestBody body = NetApi.postRequestObjBody(null,IdParam.create(73149));
//        DefaultHttp.post(UserApi.service.getUserInfo(token, body), dataView, (response, isSuccess, data) -> {
//            data.name = data.name + " come here";
//            viewModel.setUserInfo(data);
//        });
        // 用法8 全局携带头部token
        RequestBody body = NetApi.postRequestObjBody(null, IdParam.create(73149));
        DefaultHttp.postWithAuth(UserApi.service.getUserInfo(body), dataView, (response, isSuccess, data) -> {
            data.name = data.name + " come here";
            viewModel.setUserInfo(data);
        });
    }
}
